package awt.awt05.game_00;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class handDraw {

    private Frame fr=new Frame("手绘程序");

    private final int AREA_WIDTH=500;
    private final int AREA_HEIGHT=400;


    private PopupMenu colorMenu=new PopupMenu();
    private MenuItem redItem=new MenuItem("红色");
    private MenuItem greenItem=new MenuItem("绿色");
    private MenuItem blueItem=new MenuItem("蓝色");

    // 定义一个变量 记录当前画笔的颜色
    private Color forceColor=Color.BLACK;

    //记录鼠标拖到过程中上一次所处的坐标
    private int preX=-1;
    private int preY=-1;

    // 创建一个BufferImage位图对象
    BufferedImage image=new BufferedImage(AREA_WIDTH,AREA_HEIGHT,BufferedImage.TYPE_INT_RGB); // 参数为宽高和图片类别

    // 通过位图 获取关联的Graphics对象
    Graphics g=image.getGraphics();

    private class NyCanvas extends Canvas{
        @Override
        public void paint(Graphics g){
            g.drawImage(image,0,0,null);
        }
    }

    NyCanvas drawArea=new NyCanvas();


    public void init(){

        ActionListener listener=new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand=e.getActionCommand();
                System.out.println(actionCommand);
                switch(actionCommand){
                    case "红色":
                        forceColor=Color.RED;
                        break;
                    case "绿色":
                        forceColor=Color.GREEN;
                        break;
                    case "蓝色":
                        forceColor=Color.blue;
                        break;
                }
            }
        };

        redItem.addActionListener(listener);
        greenItem.addActionListener(listener);
        blueItem.addActionListener(listener);

        colorMenu.add(redItem);
        colorMenu.add(greenItem);
        colorMenu.add(blueItem);

        drawArea.add(colorMenu);

        drawArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                boolean popupTrigger=e.isPopupTrigger();
                if(popupTrigger){
                    colorMenu.show(drawArea,e.getX(),e.getY());
                }

                // 重置preX和preY
                preX=-1;
                preY=1;
            }
        });

        // 设置位图的背景为白色
        g.setColor(Color.white);
        g.fillRect(0,0,AREA_WIDTH,AREA_HEIGHT);

        //监听鼠标的移动 完成线条的绘制
        drawArea.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {

                // 没有前面的点就不能画线也就是没画过 所以要判断一下
                if(preX>0&&preY>0){
                    // 画线条需要两组坐标 分别是线条的起点和终点 e.getX() e.getY()可以获取坐标
                    g.setColor(forceColor);
                    g.drawLine(preX,preY,e.getX(),e.getY());
                }

                // 更新preX和preY的值
                preX=e.getX();
                preY=e.getY();

                // 重绘组件
                drawArea.repaint();

            }
        });

        drawArea.setPreferredSize(new Dimension(AREA_WIDTH,AREA_HEIGHT));
        fr.add(drawArea);
        fr.pack();
        fr.setVisible(true);
    }

    public static void main(String[] args){
        new handDraw().init();
    }
}
















